package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.OrdersettingDao;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Eric
 * @Create 2020-09-18  18:34
 * @Version project-workspace-0912
 **/
//interfaceClass = OrdersettingService.class 进一步定制注册到zookeeper上的服务类的接口
@Service(interfaceClass = OrdersettingService.class)
@Transactional
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrdersettingDao ordersettingDao;

    @Override
    public void upload(ArrayList<OrderSetting> orderSettings) {

        //1，遍历orderSettings
        for (OrderSetting orderSetting : orderSettings) {
            // 判断当前的日期之前是否已经被设置过预约日期，使用当前时间作为条件查询数量
            long count = ordersettingDao.findCountByOrderDate(orderSetting.getOrderDate());
            // 如果设置过预约日期，更新number数量
            if(count > 0)
            {
                ordersettingDao.editNumberByOrderDate(orderSetting);
            }else{
                // 如果没有设置过预约日期，执行保存
                ordersettingDao.add(orderSetting);
            }
        }
    }

    /**
     * 传递的参数
     * date(格式 2019-9)
     * 构建的数据List<Map>
     *     map.put("date",2019-9);
     *     map.put("number",100);
     *     map.put("reservations",10);
     *
     *     查询方案1：
     *     SELECT * FROM t_ordersetting WHERE orderDate LIKE '2019-08-%'
     *     查询方案2：
     *      SELECT * FROM t_ordersetting WHERE orderDate BETWEEN '2019-9-1' AND '2019-9-31'
     * @param date
     * @return
     */

    //根据日期查询预约设置数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //1.查询Map,   dateBegin表示月份开始时间，dateEnd表示月份结束时间
        String  dateBegin = date + "-1";//2019-9-1
        String dateEnd = date + "-31";  //2019-9-30   第31天不显示
        Map<String,Object> map = new HashMap<>();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        //2.查询当前月分的预约设置
        List<OrderSetting> list = ordersettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        //3.将List<OrderSetting> , 整理为 List<Map>
        for (OrderSetting orderSetting : list) {
            Map<String,Object> orderSettingMap = new HashMap<>();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期
            orderSettingMap.put("number",orderSetting.getNumber());
            orderSettingMap.put("reservations",orderSetting.getReservations());
            data.add(orderSettingMap);
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = ordersettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count > 0){
            //当前日期已经进行了预约设置，需要进行修改操作
            ordersettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //当前日期没有进行预约设置，进行添加操作
            ordersettingDao.add(orderSetting);
        }
    }


}
